package com.one.controller;

import com.one.service.CommentService;
import com.one.util.RequestReturn;
import com.one.util.RequestReturnFactory;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author: tzy
 * @create: 2022/1/18
 * @FileName: CommentController
 * @Description: 评论操作入口
 */
@Slf4j
@RestController
public class CommentController {
    @Autowired
    private CommentService commentService;
    /**
     * @author: tzy
     * @date: 2022/1/18
     * @return:
     * @Description: 增加评论
     */
    @GetMapping("/addComment")
    public RequestReturn<String> addComment(@RequestParam(value = "commentImage",required = false) String commentImage,
                                             @RequestParam("content") String content,
                                             @RequestParam("ordersId") Integer ordersId){
        //调用service接口
       int a = commentService.addComment(commentImage,content,ordersId);
       if (a == 1){
           return RequestReturnFactory.success("追加评论成功！！当前用户积分为：" + commentService.findUser(ordersId).getIntegral());
       }else if(a == 2){
           return RequestReturnFactory.success("发表主评论成功！！当前用户积分为：" + commentService.findUser(ordersId).getIntegral());
        }else if (a ==3){
           return RequestReturnFactory.success("第一次发表主评论成功！！获得10积分。当前用户积分为：" + commentService.findUser(ordersId).getIntegral());
       }
        return RequestReturnFactory.fail("评论失败！！订单未收货，没有权限！当前用户积分为：" + commentService.findUser(ordersId).getIntegral());
    }

    /**
     * @author: tzy
     * @date: 2022/1/19
     * @return:
     * @Description: 删除评论
     */
    @GetMapping("/removeComment")
    public RequestReturn<String> removeComment(@RequestParam("commentId") Integer commentId){
        //调用service接口
        int a = commentService.removeComment(commentId);
        if (a == 0){
            return RequestReturnFactory.fail("删除评论失败！！此评论不存在！");
        }
        return RequestReturnFactory.success("删除评论成功！！");
    }
}
